package com.fs.sys.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fs.sys.bean.dto.RoleAppDTO;
import com.fs.sys.bean.query.RoleAppQuery;
import com.fs.sys.bean.req.AppRoleAddReq;
import com.fs.sys.bean.req.RoleAppAddReq;
import com.fs.sys.entityservice.RoleAppEntityService;
import com.fs.common.annotations.Log;
import com.fs.common.bean.LogType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色应用关系控制器 
 * @author Coder Generator 2025-06-10 10:58:46 
 **/
@RestController
@RequestMapping("/sys/role-app")
@Slf4j
@Tag(name = "角色应用关系控制器")
public class RoleAppController {
    @Autowired
    private RoleAppEntityService service;

    @PostMapping("add")
    @Operation(summary = "新增")
    @Log(value = "角色应用", op = LogType.NEW)
    public void add(@RequestBody @Validated RoleAppDTO req) {
        service.insert(req);
    }

    @PutMapping("update")
    @Operation(summary = "更新")
    @Log(value = "角色应用", op = LogType.EDIT)
    public void update(@RequestBody @Validated RoleAppDTO req) {
        service.update(req);
    }

    @PostMapping("save")
    @Operation(summary = "保存")
    @Log(value = "角色应用", op = LogType.EDIT)
    public RoleAppDTO save(@RequestBody @Validated RoleAppDTO req) {
        if (StringUtils.isBlank(req.getId())) {
            return service.insert(req);
        }
        service.update(req);
        return req;
    }

    @PostMapping("save-app-roles")
    @Log(value = "角色应用", op = LogType.EDIT)
    public void saveAppRoles(@RequestBody AppRoleAddReq req) {
        service.updateAppRole(req);
    }

    @PostMapping("add-role-apps")
    @Log(value = "角色应用", op = LogType.EDIT)
    public void addRoleApps(@RequestBody RoleAppAddReq req) {
        service.addRoleApps(req);
    }

    @PostMapping("remove-role-apps")
    @Log(value = "角色应用", op = LogType.DELETE)
    public void removeRoleApps(@RequestBody RoleAppAddReq req) {
        service.deleteBuilder()
                .eq("role_id", req.getRoleId())
                .in("app_id", req.getAppIds())
                .delete();
    }

    @DeleteMapping("delete/{id}")
    @Operation(summary = "删除")
    @Log(value = "角色应用", op = LogType.DELETE)
    public void delete(@PathVariable("id") String id) {
        service.delete(id);
    }

    @GetMapping("detail/{id}")
    @Operation(summary = "根据id查找记录")
    public RoleAppDTO findById(@PathVariable("id") String id) {
        return service.findById(id).orElse(null);
    }

    @PostMapping("page-query")
    @Operation(summary = "查询-分页")
    public IPage<RoleAppDTO> pageQuery(@RequestBody RoleAppQuery query) {
        return service.pageQuery(query);
    }

    @PostMapping("query")
    @Operation(summary = "查询-不分页")
    public List<RoleAppDTO> query(@RequestBody RoleAppQuery query) {
        return service.query(query);
    }
}